#include <include/sdk/stats.h>
#include <sdk/misc.h>
#include "Level.h"

namespace skill {
	bool Evolve::canUse(Game *g, const std::vector<std::string> &params) {
		int type = -1;
		if (params.size()) {
			type = std::stoi(params[0]);
		}
        if ((type < -1)||(type > RobotTypeNum)) {
            return false;
        }
        int now_type = std::stoi(origin->properties["type"]);
        if (type == -1) type = now_type;
        if (type != now_type && now_type != 0) return false;
        int level = std::stoi(origin->properties["level"]);
        int thisLevelConsumption = stats::getLevelUPConsumption(level);
        int nextLevelConsumption = stats::getLevelUPConsumption(level + 1);
        int cost = nextLevelConsumption - thisLevelConsumption;
        int energy = std::stoi(origin->properties["energy"]);
        if (energy < cost) return false;
        return true;
	}
	
	bool Evolve::use(Game *g, const std::vector<std::string> &params) {
		int type = 0;
		if (params.size()) {
			type = std::stoi(params[0]);
		}
        int now_type = std::stoi(origin->properties["type"]);
        if (type == -1) type = now_type;
        origin->properties["type"] = std::to_string(type);
        int level = std::stoi(origin->properties["level"]);
        int thisLevelConsumption = stats::getLevelUPConsumption(level);
        int nextLevelConsumption = stats::getLevelUPConsumption(level + 1);
        int cost = nextLevelConsumption - thisLevelConsumption;
        int energy = std::stoi(origin->properties["energy"]);
        origin->properties["energy"] = std::to_string(energy - cost);
        origin->properties["level"] = std::to_string(level + 1);
        double hpRate = std::stod(origin->properties["hp"]) / std::stod(origin->properties["max_hp"]);
        origin->useSkill("ReCalculateStats", {});
        origin->properties["hp"] = std::to_string(int(std::stod(origin->properties["max_hp"]) * hpRate));
        return true;
	}
	
	bool Vestigial::canUse(Game *g, const std::vector<std::string> &params) {
		int type = -1;
		if (params.size()) {
			type = std::stoi(params[0]);
		}
        int now_type = std::stoi(origin->properties["type"]);
        if (type == -1) type = now_type;
        int level = std::stoi(origin->properties["level"]);
        if (level == 0) {
            return false;
        }
        if (level == 1 && type != RawRobot){
            return false;
        }
        if ((type < -1)||(type > RobotTypeNum)) {
            return false;
        }
		return true;
	}
	
	bool Vestigial::use(Game *g, const std::vector<std::string> &params) {
		int type = -1;
		if (params.size()) {
			type = std::stoi(params[0]);
		}
        int now_type = std::stoi(origin->properties["type"]);
        if (type == -1) type = now_type;
        origin->properties["type"] = std::to_string(type);
        int level = std::stoi(origin->properties["level"]);
        int thisLevelConsumption = stats::getLevelUPConsumption(level);
        int nextLevelConsumption = stats::getLevelUPConsumption(level - 1);
        int cost = thisLevelConsumption - nextLevelConsumption;
        int energy = std::stoi(origin->properties["energy"]);
        origin->properties["energy"] = std::to_string(energy + int(cost * VestigialLost + EPS));
        origin->properties["level"] = std::to_string(level - 1);
        double hpRate = std::stod(origin->properties["hp"]) / std::stod(origin->properties["max_hp"]);
        origin->useSkill("ReCalculateStats", {});
        origin->properties["hp"] = std::to_string(int(std::stod(origin->properties["max_hp"]) * hpRate));
        return true;
	}
}
